package com.mxx.v3.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mxx.v3.dao.mapper.TQualityMouthUnqualifiedMapper;
import com.mxx.v3.dto.QualityMouthUnqualifiedChildDto;
import com.mxx.v3.dto.QualityMouthUnqualifiedDto;
import com.mxx.v3.pojo.TQualityMouthUnqualified;
import com.mxx.v3.pojo.TQualityMouthUnqualifiedExample;
import com.mxx.v3.service.QualityMouthUnqualifiedService;
import com.mxx.v3.utils.PageQuery;
import com.mxx.v3.web.query.QualityMouthUnqualifiedQuery;
import org.apache.commons.collections.map.HashedMap;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class QualityMouthUnqualifiedImpl implements QualityMouthUnqualifiedService {
    @Autowired
    TQualityMouthUnqualifiedMapper tQualityMouthUnqualifiedMapper;
    @Override
    public PageInfo<QualityMouthUnqualifiedDto> getData(PageQuery<QualityMouthUnqualifiedQuery> qualityMouthUnqualifiedQueryPageQuery){
        // 设置分页拦截器
        Page<TQualityMouthUnqualified> tQualityMouthUnqualified = PageHelper.startPage(qualityMouthUnqualifiedQueryPageQuery.getPage(), qualityMouthUnqualifiedQueryPageQuery.getLimit(), true);
        //设置查询条件
        TQualityMouthUnqualifiedExample tQualityMouthUnqualifiedExample=new TQualityMouthUnqualifiedExample();
        TQualityMouthUnqualifiedExample.Criteria tQualityMouthUnqualifiedCriteria=tQualityMouthUnqualifiedExample.createCriteria();
        QualityMouthUnqualifiedQuery qualityMouthQualifiedQuery=qualityMouthUnqualifiedQueryPageQuery.getQueryItem();
        //判断数据不为空
        if(!StringUtils.isEmpty(qualityMouthQualifiedQuery.getProductName()))
        {
            tQualityMouthUnqualifiedCriteria.andProductNameLike("%"+qualityMouthQualifiedQuery.getProductName()+"%");
        }
        if(!StringUtils.isEmpty(qualityMouthQualifiedQuery.getProductCode()))
        {
            tQualityMouthUnqualifiedCriteria.andProductCodeLike("%"+qualityMouthQualifiedQuery.getProductCode()+"%");
        }
        if(!StringUtils.isEmpty(qualityMouthQualifiedQuery.getMonth()))
        {
            String[] date=qualityMouthQualifiedQuery.getMonth().split("-");

            tQualityMouthUnqualifiedCriteria.andTimeLike(date[0]+"-"+date[1]+"%");
        }
        //从数据库中找寻对应数据
        List<TQualityMouthUnqualified> tQualityMouthUnqualifieds=tQualityMouthUnqualifiedMapper.selectByExample(tQualityMouthUnqualifiedExample);
        Map<String,List<TQualityMouthUnqualified>> allWorkpice=getAllWorkpiece(tQualityMouthUnqualifieds);
        //创建返回的对象
        List<QualityMouthUnqualifiedDto> qualityMouthUnqualifiedDtos=new ArrayList<>();
        //循环出每个工件
        for(List<TQualityMouthUnqualified> item:allWorkpice.values())
        {
            qualityMouthUnqualifiedDtos.add(getData(item));
        }
//        //组装数据，根据分页的大小来返回数据
//        QualityMouthUnqualifiedDto  qualityMouthUnqualifiedDto= getData(tQualityMouthUnqualifieds,getAllProcess(tQualityMouthUnqualifieds));
//
//        qualityMouthUnqualifiedDtos.add(qualityMouthUnqualifiedDto);
        PageInfo<QualityMouthUnqualifiedDto> pageInfo = new PageInfo<>(qualityMouthUnqualifiedDtos);
        pageInfo.setTotal(tQualityMouthUnqualified.getTotal());
        return pageInfo;
      //  return null;
    }
    //查询所有的工件
    private Map<String,List<TQualityMouthUnqualified>> getAllWorkpiece(List<TQualityMouthUnqualified> tQualityMouthUnqualifieds){
       //对每个工件的数据分别装载
        Map<String,List<TQualityMouthUnqualified>> workpieceListMap=new HashMap<>();
        for  (TQualityMouthUnqualified t_QMU_item: tQualityMouthUnqualifieds) {
            if(workpieceListMap.containsKey(t_QMU_item.getProductCode()))
            {
                workpieceListMap.get(t_QMU_item.getProductCode()).add(t_QMU_item);
            }
            else
            {
                List<TQualityMouthUnqualified> temp=new ArrayList<>();
                temp.add(t_QMU_item);
                workpieceListMap.put(t_QMU_item.getProductCode(),temp);
            }
        }
        return workpieceListMap;
    }
    //这是获取所有工序的方法，返回按工序排列的map
    private Map<String,Integer> getAllProcess(List<TQualityMouthUnqualified> tQualityMouthUnqualifieds){
        Map<String,String> processMap=new HashMap<>();
        for (TQualityMouthUnqualified t_QMU_item: tQualityMouthUnqualifieds) {
            processMap.put(t_QMU_item.getProcessName(),t_QMU_item.getProcessCode());
        }
        //这一步是为了把工序的顺序排列一下
        Object[] objSort= processMap.keySet().toArray();
        //创建一个根据工序编码排序的map,key是工序名称，value是序号;
        Map<String,Integer> processSortMap=new HashMap<>();
        for(int i=0;i<objSort.length;i++)
        {
            int temp=i;
          processSortMap.put(objSort[temp].toString(),temp);
        }
        return processSortMap;
    }
    //根据查询出的数据，组装前端需要的对象
    private  QualityMouthUnqualifiedDto getData(List<TQualityMouthUnqualified> tQualityMouthUnqualifieds){
        //筛选出所有工序
        Map<String,Integer> processSortMap=getAllProcess(tQualityMouthUnqualifieds);
        //组装出所有的工序
        String[] process=new String[processSortMap.size()];
        for(Map.Entry<String, Integer> entry:processSortMap.entrySet())
        {
            process[entry.getValue()]=entry.getKey();
        }
        //合格的列表
        String[] quality=new String[processSortMap.size()];
        //不合格的列表
        String[] unquality=new String[processSortMap.size()];
        //合格率的列表
        String[] percent_of_quality=new String[processSortMap.size()];
        List<QualityMouthUnqualifiedChildDto> childDtos=new ArrayList<>();
        QualityMouthUnqualifiedChildDto childDto_quality=new QualityMouthUnqualifiedChildDto();
        QualityMouthUnqualifiedChildDto childDto_unquality=new QualityMouthUnqualifiedChildDto();
        QualityMouthUnqualifiedChildDto childDto_percent_of_quality=new QualityMouthUnqualifiedChildDto();
        Integer sum_quality=0,sum_unquality=0;
        double   sum_percent_of_quality=0;
        DecimalFormat df=new DecimalFormat("0.00");//设置保留位数
        //创建返回对象
        QualityMouthUnqualifiedDto qualityMouthUnqualifiedDto=new QualityMouthUnqualifiedDto();
        //开始轮询所有信息
        for(TQualityMouthUnqualified tQualityMouthUnqualified:tQualityMouthUnqualifieds){
            qualityMouthUnqualifiedDto.setProductCode(tQualityMouthUnqualified.getProductCode());
            qualityMouthUnqualifiedDto.setProductName(tQualityMouthUnqualified.getProductName());
            quality[processSortMap.get(tQualityMouthUnqualified.getProcessName())]=tQualityMouthUnqualified.getQualifiednum().toString();
            sum_quality+=tQualityMouthUnqualified.getQualifiednum();
            unquality[processSortMap.get(tQualityMouthUnqualified.getProcessName())]=tQualityMouthUnqualified.getUnqualifiednum().toString();
            sum_unquality=sum_unquality+tQualityMouthUnqualified.getUnqualifiednum();
            //计算所有的数据总数

            int sum=tQualityMouthUnqualified.getQualifiednum()+tQualityMouthUnqualified.getUnqualifiednum();
            percent_of_quality[processSortMap.get(tQualityMouthUnqualified.getProcessName())]=df.format((double)tQualityMouthUnqualified.getQualifiednum()/sum*100)+"%";
            sum_percent_of_quality+=(double)tQualityMouthUnqualified.getQualifiednum()/sum*100;
        }
        //计算平均数据
        qualityMouthUnqualifiedDto.setProcess(process);

        //下面是合格数据
        childDto_quality.setItem("合格");
        childDto_quality.setProcess(quality);
        childDto_quality.setSum(sum_quality.toString());
        childDtos.add(childDto_quality);

        childDto_unquality.setItem("不合格");
        childDto_unquality.setProcess(unquality);
        childDto_unquality.setSum(Integer.toString(sum_unquality));
        childDtos.add(childDto_unquality);

        childDto_percent_of_quality.setItem("合格率");
        childDto_percent_of_quality.setProcess(percent_of_quality);
        int sum=sum_unquality+sum_quality;
        childDto_percent_of_quality.setSum(df.format((double) sum_quality/sum*100)+"%");
        childDtos.add(childDto_percent_of_quality);
        qualityMouthUnqualifiedDto.setChild(childDtos);
        return qualityMouthUnqualifiedDto;
    }
}
